package com.lj.admin.controller.system;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lj.common.annotation.datascope.DataScope;
import com.lj.common.core.ajax.Result;
import com.lj.common.core.controller.BaseController;
import com.lj.common.core.model.system.SysDept;
import com.lj.system.constants.DeptConstants;
import com.lj.system.service.ISysDeptService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门前端控制器
 *
 * @author 小李飞刀
 */
@RequestMapping("/system/dept")
@RestController
public class SysDeptController extends BaseController {

    @Resource
    private ISysDeptService deptService;


    /**
     * 获取部门下拉树列表
     *
     * @param dept 请求参数
     * @return 下拉树
     */
    @DataScope
    @GetMapping("/treeSelect")
    public Result treeSelect(SysDept dept) {
        QueryWrapper<SysDept> wrapper = getQueryWrapper(dept);
        List<SysDept> deptList = deptService.list(wrapper);
        return Result.success(deptService.buildDeptTreeSelect(deptList));
    }

    /**
     * 获取查询条件
     *
     * @param dept 条件
     * @return 结果
     */
    private QueryWrapper<SysDept> getQueryWrapper(SysDept dept) {
        QueryWrapper<SysDept> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(dept.getDeptId()), DeptConstants.COLUMN_DEPT_ID, dept.getDeptId());
        wrapper.eq(ObjectUtil.isNotNull(dept.getParentId()), DeptConstants.COLUMN_PARENT_ID, dept.getParentId());
        wrapper.like(ObjectUtil.isNotNull(dept.getDeptName()), DeptConstants.COLUMN_DEPT_NAME, "%" + dept.getDeptName() + "%");
        wrapper.eq(CharSequenceUtil.isNotEmpty(dept.getStatus()), DeptConstants.COLUMN_STATUS, dept.getStatus());
        return wrapper;
    }
}
